﻿using RabbitDownload.Helper.Enhance.SqlLite;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RabbitDownload.WebSite
{
    internal class SiteSqlliteTool
    {
        public const string DataBaseName = "ImgInfo";
        public const string PixivTabeName = "PixivImgInfo";

        /// <summary>
        /// 初始化数据库
        /// </summary>
        public void InitSqllite()
        {
            if (new SqlliteHelper().ExitDabaBases(DataBaseName, true))
            {
                InitTable(PixivTabeName);
            }
        }

        /// <summary>
        /// 初始化表格
        /// </summary>
        public void InitTable(string TableName)
        {
            //需要有PIXIVID  所有图片LIST  评分 图片数量 标签列表
            new SqlliteHelper().ExecuteNonQuery(DataBaseName, string.Format("CREATE TABLE {0} (Id INTEGER PRIMARY KEY AUTOINCREMENT,PixivId TEXT, PixivImg TEXT ,PixivScore Text,PixivTag Text)", TableName));
        }

        /// <summary>
        /// 插入P站数据
        /// </summary>
        /// <param name="PixivId"></param>
        /// <param name="PixivImg"></param>
        /// <param name="PixivScore"></param>
        /// <param name="PixivTag"></param>
        public void InsertPixivData(string PixivId, string PixivImg, string PixivScore, string PixivTag)
        {
            if (ExitsPixivID(PixivId))
            {
                Console.WriteLine("图片{0}已存在数据库，无需重复写入。", PixivId);
                return;
            }
            //类型1是原图，类型2是zip动图
            PixivImg = PixivImgShort(PixivImg);
            string Insert = "INSERT INTO  {0} VALUES(null,\"{1}\",\"{2}\",\"{3}\",\"{4}\");";
            new SqlliteHelper().ExecuteNonQuery(DataBaseName, string.Format(Insert, PixivTabeName, PixivId, PixivImg, PixivScore, PixivTag));
        }

        /// <summary>
        /// P站图瘦身
        /// </summary>
        /// <param name="PixivImg"></param>
        /// <returns></returns>
        private static string PixivImgShort(string PixivImg)
        {
            PixivImg = PixivImg.Replace("https://i.pximg.net/img-original/img/", "type1");
            PixivImg = PixivImg.Replace("https://i.pximg.net/img-zip-ugoira/", "type2");
            return PixivImg;
        }

        /// <summary>
        /// 判断新浪图是否在数据库中
        /// </summary>
        /// <param name="SinaImgLink"></param>
        /// <returns></returns>
        public bool ExitsPixivID(string PixivId)
        {
            return DataExits(SelectPixivTableHelper("PixivId", PixivId));
        }

        /// <summary>
        /// 复用的判断
        /// </summary>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        private static bool DataExits(DataTable dataTable)
        {
            if (dataTable.Rows.Count > 0)
            {
                return true;
            }

            return false;
        }

        /// <summary>
        ///选择图片链接
        /// </summary>
        /// <param name="EnTag"></param>
        /// <returns></returns>
        public string SelectImgLink(string PixivId)
        {
            DataTable dataTable = SelectPixivTableHelper("PixivId", PixivId);

            if (dataTable == null)
                return "";
            foreach (DataRow item in dataTable.Rows)
            {
                string PixivImg = item["PixivImg"].ToString();
                PixivImg = PixivImg.Replace("type1", "https://i.pximg.net/img-original/img/");
                PixivImg = PixivImg.Replace("type2", "https://i.pximg.net/img-zip-ugoira/");

                if (PixivImg != "")
                    return PixivImg;
            }
            return "";
        }

        /// <summary>
        /// 更新图片评分
        /// </summary>
        /// <param name="PixivId"></param>
        /// <param name="ImgScore"></param>
        public void UpDateImgScore(string PixivId, string ImgScore)
        {
            string SqlText = "Update '{0}' Set 'PixivScore'='{1}' Where PixivId=='{2}'";
            //    Console.WriteLine(string.Format(SqlText, PixivTabeName, ImgScore, PixivId));
            new SqlliteHelper().ExecuteNonQuery(DataBaseName, string.Format(SqlText, PixivTabeName, ImgScore, PixivId));
        }

        /// <summary>
        /// 更新图片链接
        /// </summary>
        /// <param name="PixivId"></param>
        /// <param name="ImgScore"></param>
        public void UpDateImgLink(string PixivId, string ImgLink, string ImgScore)
        {
            ImgLink = PixivImgShort(ImgLink);
            string SqlText = "Update '{0}' Set 'PixivScore'='{1}' , 'PixivImg'='{2}' Where PixivId=='{3}'";

            new SqlliteHelper().ExecuteNonQuery(DataBaseName, string.Format(SqlText, PixivTabeName, ImgScore, ImgLink, PixivId));
        }

        /// <summary>
        /// 选择数据库内容
        /// </summary>
        /// <param name="WhereValue"></param>
        /// <param name="WhereRowName"></param>
        /// <returns></returns>
        private static DataTable SelectPixivTableHelper(string WhereRowName, string WhereValue)
        {
            string Select = "SELECT * FROM  '{0}' WHERE {1}='{2}'";
            //     Console.WriteLine(string.Format(Select, PixivTabeName, WhereRowName, WhereValue));
            DataTable dataTable = new SqlliteHelper().ExecuteReader(DataBaseName, string.Format(Select, PixivTabeName, WhereRowName, WhereValue));

            return dataTable;
        }
    }
}